package com.appkubes.mall.admin.application.user.account.history.dao;

import com.appkubes.mall.admin.application.user.account.history.entity.dto.UserAccountHistoryInfoDTO;
import com.appkubes.mall.admin.application.user.account.history.entity.vo.UserAccountHistoryVO;
import com.appkubes.mall.core.dao.BaseMallUserAccountHistoryDao;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserAccountHistoryDao extends BaseMallUserAccountHistoryDao {

    /**
     * 获取用户的收益信息表, 用于后台->运营管理->经纪人收益明细
     *
     * @param page
     * @param accountHistoryV
     * @return
     */
    @Select({"<script>",
            "SELECT history.*", //历史信息
            "       ,`user`.name, `user`.tel ", //用户信息
            "       , `order`.order_no, `order`.use_deposit_money as amount",  //订单信息
            "       , mp.promoter_key, mp.promoter_name", //推广员信息
            "       ,`utm`.`utm_source` ", //渠道信息
            "       , pu.tel as parent_tel, pu.user_key as parent_user_key", //上级信息
            " FROM mall_user_account_history as `history`", //账户历史信息(驱动表)
            " LEFT JOIN mall_user_hierarchy as  muh  ON `muh`.user_key=`history`.user_key", //账户对应的层级关系
            " LEFT JOIN mall_promoter as  mp  ON `mp`.promoter_key=`muh`.promoter_key", //推广员信息(渠道)
            " LEFT JOIN mall_order as `order` ON `history`.source_key=`order`.order_key", //订单
            " LEFT JOIN mall_user as `user` ON `user`.user_key=`order`.user_key",  //账户对应的用户
            " LEFT JOIN mall_user as  pu  ON `pu`.user_key=`history`.user_key", //用户的经纪人信息
            " LEFT JOIN mall_user_utm as `utm` ON `user`.user_key=`utm`.user_key", //渠道信息
            " WHERE 1=1",
            " <when test='accountHistoryVO.userName!=null'>" +
                    " AND `user`.name like '%${accountHistoryVO.userName}%'" +
                    " </when>",
            " <when test='accountHistoryVO.tel!=null'>" +
                    " AND `user`.tel like '%${accountHistoryVO.tel}%'" +
                    " </when>",
            " <when test='accountHistoryVO.utmSource!=null'>" +
                    " AND `utm`.utm_source like '%${accountHistoryVO.utmSource}%'" +
                    " </when>",
            " <when test='accountHistoryVO.userKey!=null'>" +
                    " AND `history`.user_key like '%${accountHistoryVO.userKey}%'" +
                    " </when>",
            " <when test='accountHistoryVO.beginTime!=null'>" +
                    " AND `history`.create_time &gt; to_date(#{accountHistoryVO.beginTime}, 'yyyyMMddhh24miss')" +
                    " </when>",
            " <when test='accountHistoryVO.endTime!=null'>" +
                    " AND `history`.create_time &lt; to_date(#{accountHistoryVO.endTime}, 'yyyyMMddhh24miss')" +
                    " </when>" +
            " ORDER BY history.create_time DESC",
            "</script>"})
    @Results({
            @Result(column = "source_key", property = "orderGoods", javaType = List.class,
                    many = @Many(select = "com.appkubes.mall.admin.application.order.goods.dao.OrderGoodsDao.getListByOrderKeyResult"))
    }
    )
    IPage<UserAccountHistoryInfoDTO> getAccountHistoryInfo(IPage<UserAccountHistoryInfoDTO> page,
                                                           @Param("accountHistoryVO") UserAccountHistoryVO accountHistoryV);
}
